package lib

//link to 718
func init() {
	Probs = append(Probs, Problem{
		Num:         209,
		Discription: ">=n的长度最小正数子数组",
		Level:       2,
		Labels: map[string]int{
			"滑动窗口": 1,
		},
	})
}

func MinSubArrayLen(target int, nums []int) int {
	sum := 0
	left := 0
	right := 0
	minLen := len(nums) + 1
	for right < len(nums) {
		sum += nums[right]
		for sum-nums[left] >= target && left <= right {
			sum -= nums[left]
			left++
		}
		if sum >= target {
			minLen = min(minLen, right-left+1)
		}
		right++
	}

    //注意最后minLen值没变的话，返回0
	if minLen == len(nums)+1 {
		return 0
	}

	return minLen
}